package com.iakuil.app.api.controller;


import com.iakuil.app.common.core.BaseController;
import com.iakuil.app.common.core.annotation.Log;
import com.iakuil.app.common.core.domain.Result;
import com.iakuil.app.common.core.enums.OpType;
import com.iakuil.app.common.core.util.ArrayUtils;
import com.iakuil.app.common.core.util.SpringUtils;
import com.iakuil.app.domain.dto.PressDto;
import com.iakuil.app.domain.dto.cmd.PressAdd;
import com.iakuil.app.domain.dto.cmd.PressEdit;
import com.iakuil.app.domain.dto.event.PressRemovedEvent;
import com.iakuil.app.domain.dto.query.PressQuery;
import com.iakuil.app.domain.entity.Press;
import com.iakuil.app.service.PressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 出版社 接口
 *
 * @author Kai
 * @date 2023-12-06
 */
@Tag(name = "出版社（示例）", description = "出版社接口")
@RestController
@RequestMapping("/api/v1/press")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PressController extends BaseController {
    private final PressService pressService;

    @Operation(summary = "出版社详情", description = "根据ID查询出版社。")
    @Parameter(name = "id", description = "ID", required = true, in = ParameterIn.QUERY)
    @GetMapping
    public Result<PressDto> doQuery(@RequestParam Long id) {
        Press entity = pressService.getById(id);
        return success(entity, PressDto.class);
    }

    @Operation(summary = "出版社列表", description = "分页查询出版社数据。")
    @PostMapping(value = "/list")
    public Result<PressDto> doPaging(@RequestBody PressQuery param) {
        Press entity = convert(param, Press.class);
        // 如果在JSON参数分页，需要显示申明startPage(param);
        startPage(param);
        return pageInfo(pressService.list(entity, param.getFields()))
            .map(e -> convert(e, PressDto.class));
    }

    @Operation(summary = "出版社新增", description = "新增出版社。")
    @Log(title = "出版社", type = OpType.INSERT)
    @PostMapping(value = "/create")
    public Result<Long> doCreate(@Validated @RequestBody PressAdd param) {
        Press entity = convert(param, Press.class, true);
        return success(pressService::save, entity);
    }

    @Operation(summary = "出版社修改", description = "修改出版社。")
    @Log(title = "出版社", type = OpType.UPDATE)
    @PostMapping(value = "/edit")
    public Result<Void> doEdit(@Validated @RequestBody PressEdit param) {
        Press entity = convert(param, Press.class, true);
        return success(pressService.modify(entity));
    }

    @Operation(summary = "出版社删除", description = "批量删除出版社。")
    @Parameter(name = "ids", description = "ID列表，逗号分隔，无需方括号", required = true, in = ParameterIn.QUERY)
    @Log(title = "出版社", type = OpType.DELETE)
    @PostMapping(value = "/remove")
    public Result<Void> doRemove(@RequestParam Long[] ids) {
        ids = ArrayUtils.removeNull(ids);
        if (ArrayUtils.isEmpty(ids)) {
            return error("无效的ID");
        }
        boolean removed = pressService.removeByIds(ids);
        if (removed) {
            // 发布出版社删除事件
            SpringUtils.publishEvent(new PressRemovedEvent(ids));
        }

        return success();
    }
}

